package bomberman.model;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import bomberman.MainGame;
import bomberman.runable.MyRunableFlame;
import bomberman.runable.MyRunableFlameChange;
import bomberman.runable.MyRunableFlameTouchBom;
import bomberman.runable.MyRunableFlameTouchEnemy;
import bomberman.swing.BomJPanel;
import bomberman.util.CommonTouch;
import bomberman.util.Constants;
import bomberman.util.GameMap;

/**
 * 炸弹爆炸火焰
 * @author EX-FUHUICHAO001
 * 2016年9月23日11:00:33
 */
public class Flame {
	private int x;
	private int y;
	
	// 定义火焰是否已移除
	private boolean isRemove = false;
	
	// 定义火焰粗细
	private int size = Constants.FLAME_SIZE_BIG;
	
	// 定义火焰为水平  或  竖直 
	private int style;
	
	
	public Flame(int x, int y, int style){
		this.x = x;
		this.y = y;
		this.style = style;
//		MyRunableThreadPool.getExecutorService().execute(new MyRunableFlameTouchEnemy(this));
		new Thread(new MyRunableFlameTouchEnemy(this)).start();
	}
	
	public Rectangle getRect(){
		return new Rectangle(x, y, 30, 30);
	}
	
	public void draw(Graphics g) {
		switch (style) {
			case Constants.FLAME_STYLE_VERTI:
				g.drawImage(GameMap.img_flame_verti_array[size], x, y, MainGame.jPanelIndex);
				break;
			case Constants.FLAME_STYLE_HORIZ:
				g.drawImage(GameMap.img_flame_horiz_array[size], x, y, MainGame.jPanelIndex);
				break;
			case Constants.FLAME_STYLE_MIX:
				g.drawImage(GameMap.img_flame_mix_array[size], x, y, MainGame.jPanelIndex);
				break;
			default:
				break;
		}
	}
	
	/**
	 * 移出火焰
	 */
	public void removeFlame(){
		if(BomJPanel.flames.contains(this)){
			BomJPanel.flames.remove(this);
			isRemove = true;
		}
	}
	
	/**
	 * 添加火焰
	 */
	public void addFlame(){

		// 保存本次爆炸产生的火焰集合，用于统一变弱和消失
		List<Flame> flame_templist = new ArrayList<Flame>();
		
		// 中心点（水平+竖直）
		BomJPanel.flames.add(this);
		flame_templist.add(this);
		
		/**
		 *  根据火焰长度，分别添加水平和竖直方向指定长度的火焰
		 *  思路：
		 *  1.超出窗体边界，不添加，且该方向不再继续添加
		 *  2.遇到石头，不添加，且该方向不再继续添加
		 *  3.遇到砖，不添加，砖消失，且该方向不再继续添加
		 */
		boolean up = true;
		boolean down = true;
		boolean left = true;
		boolean right = true;
		Flame flame;
		for (int i = 0; i < Constants.FLAME_LENGTH; i++) {
			
			// 向上（竖直）
			if (up && checkAllFlameTouch(x, y - 30 * ( i + 1 ))) {
				flame = new Flame(x, y - 30 * ( i + 1 ), Constants.FLAME_STYLE_VERTI);
				BomJPanel.flames.add(flame);
				flame_templist.add(flame);
			}else{
				// 检测是否移除砖
				if(up && checkSpeFlameTouchWall(x, y - 30 * ( i + 1 ))){
					Wall.findWallByXY(x, y - 30 * ( i + 1 )).removeWall();
				}
				up = false;
			}
			
			// 向下（竖直）
			if (down && checkAllFlameTouch(x, y + 30 * ( i + 1 ))) {
				flame = new Flame(x, y + 30 * ( i + 1 ), Constants.FLAME_STYLE_VERTI);
				BomJPanel.flames.add(flame);
				flame_templist.add(flame);
			}else{
				// 检测是否移除砖
				if(down && checkSpeFlameTouchWall(x, y + 30 * ( i + 1 ))){
					Wall.findWallByXY(x, y + 30 * ( i + 1 )).removeWall();
				}
				down = false;
			}

			// 向左（水平）
			if (left && checkAllFlameTouch(x - 30 * ( i + 1 ), y)) {
				flame = new Flame(x - 30 * ( i + 1 ), y, Constants.FLAME_STYLE_HORIZ);
				BomJPanel.flames.add(flame);
				flame_templist.add(flame);
			}else{
				// 检测是否移除砖
				if(left && checkSpeFlameTouchWall(x - 30 * ( i + 1 ), y)){
					Wall.findWallByXY(x - 30 * ( i + 1 ), y).removeWall();
				}
				left = false;
			}

			// 向右（水平）
			if (right && checkAllFlameTouch(x + 30 * ( i + 1 ), y)) {
				flame = new Flame(x + 30 * ( i + 1 ), y, Constants.FLAME_STYLE_HORIZ);
				BomJPanel.flames.add(flame);
				flame_templist.add(flame);
			}else{
				// 检测是否移除砖
				if(right && checkSpeFlameTouchWall(x + 30 * ( i + 1 ), y)){
					Wall.findWallByXY(x + 30 * ( i + 1 ), y).removeWall();
				}
				right = false;
			}
		}
		
		// 开启线程控制火焰变弱和消失
//		MyRunableThreadPool.getExecutorService().execute(new MyRunableFlameChange(flame_templist));
//		MyRunableThreadPool.getExecutorService().execute(new MyRunableFlame(flame_templist));
		new Thread(new MyRunableFlameTouchBom(flame_templist)).start();
		new Thread(new MyRunableFlameChange(flame_templist)).start();
		new Thread(new MyRunableFlame(flame_templist)).start();
	}
	
	
	public boolean checkAllFlameTouch(int x, int y){
		return !CommonTouch.checkIfTouchBorder(x, y) 
				&& !CommonTouch.checkFlameTouchStone(x, y) 
				&& !CommonTouch.checkFlameTouchWall(x, y);
	}
	
	public boolean checkSpeFlameTouchWall(int x, int y){
		return !CommonTouch.checkIfTouchBorder(x, y) 
				&& CommonTouch.checkFlameTouchWall(x, y);
	}
	
	
	
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public boolean isRemove() {
		return isRemove;
	}

	public void setRemove(boolean isRemove) {
		this.isRemove = isRemove;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getStyle() {
		return style;
	}

	public void setStyle(int style) {
		this.style = style;
	}
}
